<?php
	class Admin_model extends CI_Model{

/*--------------Quan ly don dat hang--------------*/		
		function xem_ddh()
		{
			$sql = "select id_ddh,ten_tour,ngay_bat_dau,ngay_ket_thuc,gia,ngay_dat,so_nguoi,dondathang.trang_thai,taikhoan.ho_ten,taikhoan.ten_tk, taikhoan.cmnd, taikhoan.sdt
			from tour,chuyendi,dondathang, taikhoan
			where tour.id_tour = chuyendi.id_tour and chuyendi.id_chuyendi = dondathang.id_chuyendi and dondathang.ten_tk = taikhoan.ten_tk and tour.trang_thai=1 and  dondathang.trang_thai=0 ";
			
			$data = $this->db->query($sql);
			return $data->result_array();
		}
		
		function timkiem_ddh($ho_ten,$trang_thai)
		{
			$sql = "select id_ddh,ten_tour,ngay_bat_dau,ngay_ket_thuc,gia,ngay_dat,so_nguoi,dondathang.trang_thai,taikhoan.ho_ten,taikhoan.ten_tk, taikhoan.cmnd, taikhoan.sdt
			from tour,chuyendi,dondathang, taikhoan
			where tour.id_tour = chuyendi.id_tour and chuyendi.id_chuyendi = dondathang.id_chuyendi and dondathang.ten_tk = taikhoan.ten_tk and tour.trang_thai=1 and  dondathang.trang_thai='".$trang_thai."' and taikhoan.ho_ten like '%".$ho_ten."%' ";
			
			$data = $this->db->query($sql);
			return $data->result_array();
		}
		
		function huy_ddh($id)
		{
			$this->db->where("id_ddh",$id);
			$this->db->delete("dondathang");
		}
/*--------------Quan ly phan hoi--------------*/		
		function xem_phanhoi()
		{
			$this->db->select("id_phanhoi,tieu_de,noi_dung,phanhoi.ten_tk,taikhoan.ho_ten");
			//$this->db->select("*");
			$this->db->from("phanhoi");
			$this->db->join("taikhoan","phanhoi.ten_tk = taikhoan.ten_tk");
			//$sql="select * from phanhoi";
			$data = $this->db->get();
			return $data->result_array();
		}
		function xoa_phanhoi($id)
		{
			$this->db->where("id_phanhoi",$id);
			$this->db->delete("phanhoi");
		}
/*--------------Quan ly khuyen mai--------------*/		
		function xem_khuyenmai()
		{
			$ngay = getdate();
			$currentday = $ngay["year"]."-".$ngay["mon"]."-".$ngay["mday"];
			$this->db->select("id_km,tieu_de,ngay_bat_dau,ngay_ket_thuc,ty_le,so_luong");
			$this->db->from("khuyenmai");
			$this->db->where("ngay_ket_thuc >= '".$currentday."'");
			$this->db->where("trang_thai",1);
			$data = $this->db->get();
			return $data->result_array();
		}
		
		function tk_khuyenmai()
		{
			$ngay = getdate();
			$currentday = $ngay["year"]."-".$ngay["mon"]."-".$ngay["mday"];
			$day = $ngay["year"]."-".$ngay["mon"]."-".$ngay["mday"];
			$this->db->select("id_km,tieu_de,ngay_bat_dau,ngay_ket_thuc,ty_le,so_luong");
			$this->db->from("khuyenmai");
			$this->db->where("ngay_ket_thuc <'".$currentday."'");
			$this->db->where("trang_thai",1);
			$data = $this->db->get();
			return $data->result_array();
		}
		function get_khuyenmai($id)
		{
			$this->db->select("id_km,tieu_de,ngay_bat_dau,ngay_ket_thuc,ty_le,so_luong");
			$this->db->from("khuyenmai");
			$this->db->where("id_km",$id);
			$data = $this->db->get();
			return $data->row();
		}
		function them_khuyenmai($data)
		{
			$this->db->insert("khuyenmai",$data);
			return $this->db->insert_id();
		}
		
		public function sua_khuyenmai($id,$data)
		{
			$this->db->where("id_km",$id);
			$this->db->update("khuyenmai",$data);
		}
		public function xoa_khuyenmai($id,$trangthai)
		{
			$this->db->where("id_km",$id);
			$this->db->update("khuyenmai",$trangthai);
		}
/*--------------Quan ly lap hoa don--------------*/	
		function kh_chuatt()
		{
			$this->db->select("*");
			$this->db->from("dondathang");
			$this->db->join("taikhoan","dondathang.ten_tk=taikhoan.ten_tk");
			$this->db->where("dondathang.trang_thai",0);
			$data = $this->db->get();
			return $data->result_array();
		}
		function timkiem_khachhang($ho_ten)
		{
			$this->db->select("*");
			$this->db->from("taikhoan");
			$this->db->like("ho_ten",$ho_ten);
			$this->db->where("trang_thai",1);
			$data = $this->db->get();
			return $data->result_array();
		}
			
		function xemddh_hd($ten_tk)
		{
			$this->db->select("*");
			$this->db->from("dondathang");
			$this->db->join("chuyendi","dondathang.id_chuyendi=chuyendi.id_chuyendi");
			$this->db->join("taikhoan","dondathang.ten_tk=taikhoan.ten_tk");
			$this->db->where("dondathang.ten_tk",$ten_tk);
			$this->db->where("dondathang.trang_thai",0);
			$data = $this->db->get();
			return $data->result_array();
		}
		function xemchitiet_ddh($id_ddh)
		{
			$this->db->select("*");
			$this->db->from("dondathang");
			$this->db->join("chuyendi","dondathang.id_chuyendi=chuyendi.id_chuyendi");
			$this->db->join("tour","chuyendi.id_tour=tour.id_tour");
			$this->db->join("taikhoan","dondathang.ten_tk=taikhoan.ten_tk");
			$this->db->where("dondathang.id_ddh",$id_ddh);
			$this->db->where("dondathang.trang_thai",0);
			$data = $this->db->get(
			);
			return $data->result_array();
		}
/*--------------Quan ly hoa don--------------*/	
		function tk_hoadonkh($ho_ten)
		{
			$this->db->select("*");
			$this->db->from("hoadon");
			$this->db->join("taikhoan","taikhoan.ten_tk=hoadon.ten_tk");
			$this->db->like("ho_ten",$ho_ten);
			$this->db->where("trang_thai",1);
			$data = $this->db->get();
			return $data->result_array();
		}
	}

	?>